package com.huawei.classroom.student.h17;

public class GameObject {
	private Player player;
	private int x;
	private int y;
	private int range;
	private int strength;
	private int health;
	
	public GameObject(Player player, int x, int y, int range, int strength, int health) {
		this.player = player;
		this.x = x;
		this.y =y;
		this.range =range;
		this.strength = strength;
		this.health = health;
	}
	
	public GameObject() {
		
	}
	
	public int getHealth() {
		return health;
	}
	
	public void setHealth(int health) {
		this.health = health;
	}
	
	public int getRange() {
		return range;
	}
	
	public int getStrength() {
		return strength;
	}

	public int getX() {
		return x;
	}

	public int getY() {
		return y;
	}
	
	public Player getplayer() {
		return player;
	}
	
	public boolean isDestroyed() {
		return health <= 0;
	}
	
	public void move(int dx, int dy) {
		this.x = this.x + dx;
		this.y = this.y + dy;
	}
	
	public void attack(GameObject object) {
		if(health <= 0) {//判断生命
			return;
		}
		if(getDistance(object) - range > 0.0001 || object.getHealth() <= 0) {//判断距离和对象生命
			return;
		}
		object.setHealth(object.getHealth() - strength);
	}
	
	public double getDistance(GameObject object) {
		return Math.sqrt((x-object.getX())*(x-object.getX())*1.0+(y-object.getY())*(y-object.getY())*1.0);
	}
	
	public void attack() {
		if(isDestroyed()) {
			return;
		}
		if(range == 0) {//不给那三个东西攻击
			return;
		}
		double mindistance = 10000;
		GameObject minobj = null;
		for(int i=0; i<BattleField.gameObjects.size(); i++) {//找最近且满足条件的对象
			if(BattleField.gameObjects.get(i).getHealth() <= 0 || BattleField.gameObjects.get(i).getplayer().equals(player)) {
				continue;
			}
			if(getDistance(BattleField.gameObjects.get(i)) - range > 0.0001) {
				continue;
			}
			if(getDistance(BattleField.gameObjects.get(i)) < mindistance) {
				mindistance = getDistance(BattleField.gameObjects.get(i));
				minobj = BattleField.gameObjects.get(i);
			}
		}
		if(minobj == null)
			return;
		minobj.setHealth(minobj.getHealth() - strength);
		System.out.println(this+" "+this.getplayer().getName()+" 攻击："+minobj+" "+minobj.player.getName());
	}
}
